package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.easemob.im.server.api.message.MessageApi;
import com.tanhua.autoconfig.template.HuanXinTemplate;

import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.FriendApi;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.vo.ContactVo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.MyThreadLocal;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.w3c.dom.ls.LSInput;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class MessagesService {

    @DubboReference
    private UserApi userApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private FriendApi friendApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    /**
     * 根据环信id查询用户详情
     */
    public UserInfoVo findUserInfoByHuanxin(String huanxinId) {
        //1、根据环信id查询用户
        User user = userApi.findByHuanxin(huanxinId);
        //2、根据用户id查询用户详情
        UserInfo userInfo = userInfoApi.getById(user.getId());
        UserInfoVo vo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo,vo); //copy同名同类型的属性
        if(userInfo.getAge() != null) {
            vo.setAge(userInfo.getAge().toString());
        }
        return vo;
    }

    //添加好友
    public void contacts(Long friendId) {
        Long userId = MyThreadLocal.getUserId();
        Boolean aBoolean = huanXinTemplate.addContact(Constants.HX_USER_PREFIX + userId, Constants.HX_USER_PREFIX + friendId);
        if (!aBoolean){
            throw new BusinessException(ErrorResult.error());
        }
       friendApi.save(userId,friendId);
    }
    //删除好友
    public void removeContacts(Long friendId) {
        Long userId = MyThreadLocal.getUserId();
        Boolean aBoolean = huanXinTemplate.deleteContact(Constants.HX_USER_PREFIX + userId, Constants.HX_USER_PREFIX + friendId);
        if (!aBoolean){
            throw new BusinessException(ErrorResult.error());
        }
        friendApi.delete(userId,friendId);
    }
    //联系人列表
    public PageResult findFriends(Integer page, Integer pagesize, String keyword) {

            List<Friend> list = friendApi.findByUserId(MyThreadLocal.getUserId(), page, pagesize);
            if (CollUtil.isEmpty(list)) {
                return new PageResult();
            }
            List<Long> ids = CollUtil.getFieldValues(list, "friendId", Long.class);
            UserInfo userInfo = new UserInfo();
            userInfo.setNickname(keyword);
            Map<Long, UserInfo> map = userInfoApi.findByIds(ids, userInfo);

            ArrayList<ContactVo> vos = new ArrayList<>();
            for (Friend friend : list) {
                Long friendId = friend.getFriendId();
                UserInfo info = map.get(friendId);
                if (info != null) {
                    ContactVo vo = ContactVo.init(info);
                    vos.add(vo);
                }
            }
            return new PageResult(page, pagesize, 0, vos);
    }



}